package dao;

import entity.Report;
import util.DatabaseUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ReportDao {
    public void reportBlog(int blogId, int userId, String reason) {
        String sql = "INSERT INTO report (blogId, userId, reason) VALUES (?, ?, ?)";

        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql)) {

            pst.setInt(1, blogId);
            pst.setInt(2, userId);
            pst.setString(3,reason);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Report> getUnreviewedReports() {
        List<Report> reports = new ArrayList<>();
        String sql = "SELECT * FROM report WHERE status = 'PENDING'";

        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql)) {
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                int userId = rs.getInt("userId");
                int blogId = rs.getInt("blogId");
                int commentId = rs.getInt("commentId");
                String reason = rs.getString("reason");
                String status = rs.getString("status");
                Timestamp createdTime = rs.getTimestamp("createdTime");

                Report report = new Report(id, userId, blogId, commentId, reason, status, createdTime);
                reports.add(report);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return reports;
    }

    public void updateReportStatus(int reportId, String status) {
        String sql = "UPDATE report SET status = ? WHERE id = ?";

        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql)) {
            pst.setString(1, status);
            pst.setInt(2, reportId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void banUser(int userId) {
        String sql = "UPDATE user SET isBanned = ? WHERE id = ?";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql)) {
            pst.setBoolean(1, true); //设置为封禁状态
            pst.setInt(2, userId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void unbanUser(int userId) {
        String sql = "UPDATE user SET isBanned = ? WHERE id = ?";
        try (Connection connection = DatabaseUtil.getConnection();
             PreparedStatement pst = connection.prepareStatement(sql)) {
            pst.setBoolean(1, false);  // 设置为未封禁状态
            pst.setInt(2, userId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
